package top.infopub.mgc.core.service.impl;


import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.locks.ReentrantReadWriteLock;

import javax.annotation.PostConstruct;

import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgc.core.dao.PartnerInfoDao;
import top.infopub.mgc.core.domain.PartnerInfoDomain;
import top.infopub.mgc.core.service.PartnerInfoService;
import top.infopub.model.mgc.PartnerInfo;
import top.infopub.zookeeper.confclient.listener.ConfigurationListener;

import com.alibaba.fastjson.JSON;


@Service("partnerInfoService")
public class PartnerInfoServiceImpl extends GenericServiceImpl<PartnerInfo, String> implements PartnerInfoService, ConfigurationListener {

    private Logger log = LoggerFactory.getLogger(PartnerInfoServiceImpl.class);

    @Autowired
    private PartnerInfoDao partnerInfoDao;

    /** k-v code-bean **/
    private Map<String, PartnerInfoDomain> caches = null;

    private ReentrantReadWriteLock lock = new ReentrantReadWriteLock();

    @Override
    public GenericDao<PartnerInfo, String> getDao() {
        return partnerInfoDao;
    }

    @Override
    public PartnerInfoDomain getPartnerCache(String code) {
        if (caches == null || StringUtils.isEmpty(code)) {
            return null;
        }
        try {
            lock.readLock().lock();
            PartnerInfoDomain _d = caches.get(code);
            if (_d == null) {
                return null;
            }
            PartnerInfoDomain dest = new PartnerInfoDomain();
            BeanUtils.copyProperties(dest, _d);
            return dest;
        }
        catch (Exception e) {
            log.error("", e);
            return null;
        }
        finally {
            lock.readLock().unlock();
        }
    }

    @PostConstruct
    public void loadData() {
        List<PartnerInfo> result = null;
        try {
            lock.writeLock().lock();
            caches = new HashMap<>();
            PartnerInfo where = new PartnerInfo();
            where.setDelFlag(0);
            result = partnerInfoDao.selectByEntityWhere(where);
            initCacheInfo(result);
        }
        catch (Exception e) {
            log.error("", e);
        }
        finally {
            lock.writeLock().unlock();
        }

    }

    private void initCacheInfo(List<PartnerInfo> list)
        throws Exception {
        if (list != null && !list.isEmpty()) {
            for (PartnerInfo var : list) {
                PartnerInfoDomain domain = new PartnerInfoDomain();
                BeanUtils.copyProperties(domain, var);
                String extInfo = var.getSpartnerExtInfo();
                // 供应商类型  1=短信 2=邮箱
                if (var.getItype() == 1) {
                    PartnerInfoDomain.Sms sms = new PartnerInfoDomain.Sms();
                    sms = JSON.parseObject(extInfo, PartnerInfoDomain.Sms.class);
                    domain.setSms(sms);
                }
                else if (var.getItype() == 2) {
                    PartnerInfoDomain.Email email = new PartnerInfoDomain.Email();
                    email = JSON.parseObject(extInfo, PartnerInfoDomain.Email.class);
                    domain.setEmail(email);
                }
                caches.put(domain.getSpartnerCode(), domain);
            }
        }
    }

    @Override
    public void configurationChangedNotice(String keyName, String value) {
        // 重新加载缓存数据 
        if (ZK_NAME.equalsIgnoreCase(keyName)) {
            loadData();
        }
    }

}